const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');

//获取应用实例
const app = getApp()
var index = 1
Page({
  data: {
    message: [{
        title: '这是第一条滚动消息',
      },
      {
        title: '这是第二条滚动消息',
      },
      {
        title: '这是第三条滚动消息',
      },
      {
        title: '这是第四条滚动消息',
      },
      {
        title: '这是第五条滚动消息',
      },
    ],
    activity: [{
        name: "签到赚积分",
        url: "/pages/sign/sign",
        img: "../../static/images/integralDay.png"
      },
      {
        name: "幸运大转盘",
        url: "/pages/lotter/lotter",
        img: "../../static/images/lotteryDay.png"
      },
      // {
      //   name: "壹积分抢购",
      //   url: "/pages/z-modify/integralBuy/integralBuy",
      //   img: "../../../static/images/integral.png"
      // },
      // {
      //   name: "壹爱币抢购",
      //   url: "/pages/z-modify/coinBuy/coinBuy",
      //   img: "../../../static/images/coin.png"
      // }
    ],
    shoplist: [{
        name: "新品首发",
        url: "/pages/newGoods/newGoods",
        url1: "/static/images/shop-1.png",
        url2: "/static/images/shop-2.png"
      },
      {
        name: "人气推荐",
        url: "/pages/hotGoods/hotGoods",
        url1: "/static/images/shop-3.png",
        url2: "/static/images/shop-4.png"
      },
      {
        name: "拼团专区",
        url: "/pages/bulkPurchase/bulkPurchase",
        url1: "/static/images/shop-5.png",
        url2: "/static/images/shop-6.png"
      },
      {
        name: "品牌专区",
        url: "/pages/brand/brand",
        url1: "/static/images/shop-7.png",
        url2: "/static/images/shop-8.png"
      }
    ],
    // show: true,
    // word: '',
    goodsCount: 0,
    banner: [],
    channel: [],
    goods: [],
    loadingText: "数据加载中...",
    remindValue: ['', '', '送'],
    plusFlag: ['', '+', ' '],
    vipCategoryL2: [],
    integralCategoryL2: []
  },
  onShareAppMessage: function() {
    return {
      title: '爱联商圈小程序',
      desc: '爱联商圈小程序商城',
      path: '/pages/index/index'
    }
  },

  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({
      show: true
    });
    this.getIndexData();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  getIndexData: function() {
    let that = this
    util.request(api.IndexUrl).then(function(res) {
      if (res.errno === 0) {
        that.setData({
          goodsCount: res.data.goodsCount,
          banner: res.data.banner,
          channel: res.data.channel,
          vipCategoryL2: res.data.memberGoodsNum,
          integralCategoryL2: res.data.integralGoodsNum
        });
        var memberGoodsNum = res.data.memberGoodsNum
        var integralGoodsNum = res.data.integralGoodsNum
        wx.setStorageSync("memberGoodsNum", memberGoodsNum)
        wx.setStorageSync("integralGoodsNum", integralGoodsNum)
        app.globalData.memberGoodsNum = memberGoodsNum
        app.globalData.integralGoodsNum = integralGoodsNum
      }
    })
  },

  confirmTrue: function() {
    this.setData({
      show: false
    });
  },


  getGoodsList: function(page) {
    let that = this
    return util.request(api.HotGoods, {
      page: page,
      size: 6,
    })
  },

  onLoad: function(options) {
    this.getIndexData()
    this.getGoodsList(1).then(res => {
      this.setData({
        goods: res.data.goodsList,
        total: Math.ceil(res.data.total / 6)
      })
    })


  },

  // scroll
  onReachBottom: function() {
    index += 1
    const tempGoods = this.data.goods
    wx.showLoading({
      title: '数据加载中',
      mask: true
    })
    if (index <= this.data.total) {
      setTimeout(() => {
        this.getGoodsList(index).then(res => {
          this.setData({
            goods: tempGoods.concat(res.data.goodsList)
          })
        })
        wx.hideLoading()
      }, 500)
    } else {
      wx.showToast({
        title: '暂无更多数据',
        icon: "none"
      })
      this.setData({
        loadingText: "已无更多数据"
      })
    }

  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {

    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },
})